package com.ruoyi.app.controller;

import com.alibaba.fastjson2.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ruoyi.app.domain.*;
import com.ruoyi.app.service.IFillingService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@RestController
@RequestMapping("/app/submintted/")
public class SubmittedController extends BaseController {

    @Autowired
    private IFillingService iFillingService;
    /**
     * 获取设备
     */
    @Anonymous
    @GetMapping("/getSubmintted")
    @ResponseBody
    public AjaxResult getSubminttedData(HttpServletRequest request) {

        Area area = new Area();
        area.setStatus(0);
        List<Area> areas = iFillingService.selectAreaList(area);
        if(areas.size() > 0){
            for (Area area1:
            areas) {
                Station station = new Station();
                station.setStatus(0);
                station.setAreaId(area1.getId());
                List<Station> stations = iFillingService.selectStationList(station);
                if(stations.size() > 0){

                    for (Station station1:
                    stations) {
                        User user = new User();
                        user.setStationId(station1.getId());
                        user.setStatus(0);
                        List<User> users = iFillingService.selectUserList(user);
                        if(users.size() > 0){
                            Date da = new Date();//获取当前时间

                            Calendar calendar = Calendar.getInstance(); //得到日历
                            calendar.setTime(da);//把当前时间赋给日历
                            calendar.add(calendar.MONTH, -2);  //设置为前2月
                            Date times = calendar.getTime();//获取2个月前的时间
                            String time = new SimpleDateFormat("yyyy-MM-01").format(times);

                            String nowTime = new SimpleDateFormat("yyyy-MM-01").format(da);
                            for (User user1:
                            users) {
                                String avg = iFillingService.selectAreaListByTime(String.valueOf(user1.getId()),time,nowTime);
                                if(avg != null){
                                    user1.setAverageDegree(Float.valueOf(avg));
                                }

                            }
                            station1.setUsers(users);
                        }
                    }
                    area1.setStations(stations);
                }
            }
        }

        return AjaxResult.success("成功",areas);
    }
    @Anonymous
    @PostMapping("/addSubmintted")
    @Transactional
    @ResponseBody
    public AjaxResult addSubmintted(@RequestBody String reqParam) {

        JSONObject reqJson = JSONObject.parseObject(reqParam);

        String datas = reqJson.getString("data");
        String staffId = reqJson.getString("staffId");

        JsonArray jsonArray = new JsonParser().parse(datas).getAsJsonArray();

        int res = 0;
        for (JsonElement json : jsonArray
        ) {
            JsonObject jsonObject = json.getAsJsonObject();

            int operationId = jsonObject.get("operationId").getAsInt();
            int stationId = jsonObject.get("stationId").getAsInt();
            int userId = jsonObject.get("userId").getAsInt();
            float degrees = jsonObject.get("degrees").getAsFloat();

            Filling filling = new Filling();
            filling.setStaffId(Long.parseLong(staffId));
            filling.setOperationId(operationId);
            filling.setStationId(stationId);
            filling.setUserId(userId);

            Filling filling1 = iFillingService.selectFillingNowMonth(filling);
            if(filling1 == null){
                filling.setDegrees(degrees);
                res = iFillingService.insertFilling(filling);
            }else{
                filling1.setDegrees(degrees);
                res = iFillingService.updateFilling(filling1);
            }
        }
        if(res == 1){
            return AjaxResult.success("成功");
        }else{
            throw new RuntimeException("填报失败");
        }
    }
}
